package com.crmeb.crmebcollect;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Presentation;
import android.content.Context;
import android.content.Intent;
import android.media.MediaRouter;
import android.os.Bundle;
import android.os.RemoteException;
import android.view.Display;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.sunmi.peripheral.printer.InnerPrinterCallback;
import com.sunmi.peripheral.printer.InnerPrinterException;
import com.sunmi.peripheral.printer.InnerPrinterManager;
import com.sunmi.peripheral.printer.SunmiPrinterService;

public class MainActivity extends AppCompatActivity {

    // 商米 服务
    private SunmiPrinterService sunmiPrinterService;


    // 网页容器
    private WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化商米驱动
        initSunmiPrinterService(MainActivity.this);
        // 获取钱箱button
        findViewById(R.id.crmeb_cashopen).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this,"你点击了开启钱箱", Toast.LENGTH_LONG).show();
                openCashBox();
            }
        });

        // 本地测试WebView
        findViewById(R.id.crmeb_money_collect_local_test).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, WebViewActivity.class));
            }
        });

        // 线上测试WebView
        findViewById(R.id.crmeb_money_collect).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, CrmebCollectActicity.class));
            }
        });


        // 顾显操作
        MediaRouter mediaRouter = (MediaRouter) MainActivity.this.getSystemService(Context.MEDIA_ROUTER_SERVICE);
        MediaRouter.RouteInfo route = mediaRouter.getDefaultRoute();
        if (route != null) {
            Display presentationDisplay = route.getPresentationDisplay();
            if (presentationDisplay != null) {
                Presentation presentation = new MyPresentation(MainActivity.this, presentationDisplay);
                webView = new WebView(this);
                presentation.setContentView(webView);
                webView.setWebChromeClient(new WebChromeClient());
                webView.setWebViewClient(new WebViewClient());
                webView.loadUrl("file:///android_asset/presentation.html");
                presentation.show();
            }
        }

    }


    private final InnerPrinterCallback innerPrinterCallback = new InnerPrinterCallback() {
        @Override
        protected void onConnected(SunmiPrinterService service) {
            sunmiPrinterService = service;
//            checkSunmiPrinterService(service);
        }

        @Override
        protected void onDisconnected() {
            sunmiPrinterService = null;
//            sunmiPrinter = LostSunmiPrinter;
        }
    };

    /**
     * init sunmi print service
     */
    public void initSunmiPrinterService(Context context){
        try {
            boolean ret =  InnerPrinterManager.getInstance().bindService(context,
                    innerPrinterCallback);
            if(!ret){
//                sunmiPrinter = NoSunmiPrinter;
                Toast.makeText(MainActivity.this, "初始化钱箱服务失败", Toast.LENGTH_LONG).show();
            }
        } catch (InnerPrinterException e) {
            e.printStackTrace();
        }
    }



    public void openCashBox(){
        if(sunmiPrinterService == null){
            //TODO Service disconnection processing
            return;
        }

        try {
            sunmiPrinterService.openDrawer(null);
        } catch (RemoteException e) {
            Toast.makeText(MainActivity.this,"Exception:"+e.getMessage(),Toast.LENGTH_LONG).show();
        }
    }
}